package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.toolkit.EncryptUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.widget.EditTextWithClear;
import com.digitalchina.lxnsy.xml.UserXML;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/3/7 19:50
 * 修改备注：
 * ================================================
 */
public class UpdatePasswordActivity extends BaseActivity {

    @Bind(R.id.update_pwd_et_old_password)
    EditTextWithClear updatePwdEtOldPassword;
    @Bind(R.id.update_pwd_et_new_password)
    EditTextWithClear updatePwdEtNewPassword;
    @Bind(R.id.update_pwd_et_confirm_password)
    EditTextWithClear updatePwdEtConfirmPassword;
    @Bind(R.id.update_pwd_bt_confirm_modification)
    Button updatePwdBtConfirmModification;

    @Override
    public void setLayout() {
        setTitle("修改密码");
        setContentLayout(R.layout.activity_update_password);
    }

    @Override
    public void initMember() {

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_UPDATE_PASSWORDOLD_2_NEW_SUCCESS:
                        showToast("修改成功");
                        UserXML.setLoginPassword(UpdatePasswordActivity.this,EncryptUtil.SHA1(StringUtil.getEditText(updatePwdEtNewPassword)));
                        finish();
                        break;
                    case MSG.MSG_UPDATE_PASSWORDOLD_2_NEW_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.update_pwd_bt_confirm_modification)
    public void onClick() {
        upDatePWD();
    }

    /**
     * 修改密码
     */
    private void upDatePWD() {

        String strOldPwd = StringUtil.getEditText(updatePwdEtOldPassword);
        String strNewPwd = StringUtil.getEditText(updatePwdEtNewPassword);
        String strConfirmPwd = StringUtil.getEditText(updatePwdEtConfirmPassword);

        if (StringUtil.isStrEmpty(strOldPwd)) {
            showToast("原密码不能为空");
            return;
        }
        if (StringUtil.isStrEmpty(strNewPwd)) {
            showToast("新密码不能为空");
            return;
        }
        if (StringUtil.isStrEmpty(strConfirmPwd)) {
            showToast("确认密码不能为空");
            return;
        }
        if (!StringUtil.isStrEqual(strNewPwd, strConfirmPwd)) {
            showToast("新密码与确认密码不一致");
            return;
        }
//        if (strConfirmPwd.length() < 6 ||
//                !RegexpUtil.isRegexpValidate(strConfirmPwd, RegexpUtil.LETTER_AND_NUMBER_REGEXP)) {
//            showToast("密码为6-20个字符、字母和数字组合");
//            return;
//        }

        BusinessAccount.updatePasswordOld2New(this, strOldPwd, strConfirmPwd, mHandler);
    }
}
